//
//  CompanyInfoCertificateVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/28.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CompanyInfoCertificateVC.h"
#import "CertificationViewController.h"
#import "SearchCompanyViewController.h"
#import "CompanyModel.h"
#import "PositionSelectViewController.h"
#import "JobModel.h"
#import "SelectCityViewController.h"
#import "CityModel.h"
#import "SimpleInputVC.h"
#import "TimeSelectViewController.h"
#import "JobCertificationViewController.h"
#import "AuthenticationModel.h"
#import "AppDelegate.h"

@interface CompanyInfoCertificateVC ()

@property (nonatomic, strong) CompanyModel *company;
@property (nonatomic, strong) JobModel *job;
@property (nonatomic, strong) CityModel *city;
@property (nonatomic, copy) NSString *minTime;
@property (nonatomic, copy) NSString *maxTime;

@property (nonatomic, strong) AuthenticationModel *authenModel;

@end

@implementation CompanyInfoCertificateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"职位认证";
    
    if (self.editPage == 1) {
        [self.nextBtn setTitle:@"确定" forState:UIControlStateNormal];
        
        self.companyL.text = self.companyName;
        self.positionCategoryL.text = self.jobName;
        self.positionNameL.text = self.params[@"positionName"];
        self.cityL.text = self.regionName;
        self.timeL.text = [NSString stringWithFormat:@"%@-%@", self.minTimeStr, self.maxTimeStr];
        self.emailL.text = self.params[@"companyEmail"];
        
        self.minTime = self.minTimeStr;
        self.maxTime = self.maxTimeStr;
        
        CompanyModel *company = [[CompanyModel alloc] init];
        company.companyId = self.params[@"companyId"];
        company.companyName = self.companyName;
        self.company = company;
        
        JobModel *job = [[JobModel alloc] init];
        job.jobId = self.params[@"jobId"];
        job.jobName = self.jobName;
        self.job = job;
        
        CityModel *city = [[CityModel alloc] init];
        city.regionId = self.params[@"region"];
        city.regionName = self.regionName;
        self.city = city;
    } else {
        [self.nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [self getIncumbentAuthenticationInfo];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SearchCityBlock:) name:@"searchcity" object:nil];
}

- (void)doBack:(id)sender {
    if (self.index == 1) {  // 从注册页进来的
        [((AppDelegate *)[UIApplication sharedApplication].delegate) gotoHome];
    } else {
        if (self.editPage == 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

/** 查询在职者认证信息 */
- (void)getIncumbentAuthenticationInfo {
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest getIncumbentAuthenticationInfoWithStatus:@"INCUMBENT_IN_REVIEW" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"查询在职者认证信息 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [result objectForKey:@"data"];
            if (!datas || datas.count == 0) {
                
            } else {
                NSDictionary *dataDict = [datas objectAtIndex:0];
                
                self.authenModel = [AuthenticationModel mj_objectWithKeyValues:dataDict];
                
                [weakSelf resetViewData];
            }
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)resetViewData {
    self.companyL.text = self.authenModel.companyName;
    self.positionCategoryL.text = self.authenModel.jobName;
    self.positionNameL.text = self.authenModel.positionName;
    self.cityL.text = self.authenModel.regionName;
    self.timeL.text = self.authenModel.entryTime;
    self.emailL.text = self.authenModel.companyEmail;
    
    CompanyModel *company = [[CompanyModel alloc] init];
    company.companyId = self.authenModel.companyId;
    company.companyName = self.authenModel.companyName;
    self.company = company;
    
    JobModel *job = [[JobModel alloc] init];
    job.jobId = self.authenModel.jobId;
    job.jobName = self.authenModel.jobName;
    self.job = job;
    
    CityModel *city = [[CityModel alloc] init];
    city.regionId = self.authenModel.region;
    city.regionName = self.authenModel.regionName;
    self.city = city;
}

- (IBAction)topCloseBtnClick:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.headerHeight.constant = 0;
    }];
}

- (IBAction)companyNameBtnClick:(id)sender {
    SearchCompanyViewController *scVC = [[SearchCompanyViewController alloc] init];
    scVC.index = 1;
    scVC.companyBlock = ^(CompanyModel *company) {
        self.company = company;
        self.companyL.text = company.companyAbbreviatedName;
    };
    [self.navigationController pushViewController:scVC animated:YES];
}

- (IBAction)jobCategoryBtnClick:(id)sender {
    PositionSelectViewController *psVC = [[PositionSelectViewController alloc] init];
    psVC.selectBlock = ^(JobModel *job) {
        self.job = job;
        self.positionCategoryL.text = job.jobName;
    };
    [self.navigationController pushViewController:psVC animated:YES];
}

- (IBAction)jobNameBtnClick:(id)sender {
    SimpleInputVC *input = [[SimpleInputVC alloc] init];
    input.index = 1;
    input.saveBlock = ^(NSString *input) {
        self.positionNameL.text = input;
    };
    [self.navigationController pushViewController:input animated:YES];
}

- (IBAction)cityBtnClick:(id)sender {
    SelectCityViewController *scVC = [[SelectCityViewController alloc] init];
    scVC.cityBlock = ^(CityModel *city) {
        self.city = city;
        self.cityL.text = city.regionName;
    };
//    [self.navigationController pushViewController:scVC animated:YES];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:scVC];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)SearchCityBlock:(NSNotification *)notification {
    // 得到传递的参数
    NSDictionary *dict = [notification userInfo];
    // 重新选择收货地址后，走此条件
    CityModel *city = [dict objectForKey:@"city"];
    
    self.city = city;
    self.cityL.text = city.regionName;
}


- (IBAction)jobTimeBtnClick:(id)sender {
    TimeSelectViewController *time = [[TimeSelectViewController alloc] init];
    WS(weakSelf)
    time.timeSelectBlock = ^(NSString *minTime, NSString *maxTime) {
        weakSelf.minTime = minTime;
        weakSelf.maxTime = maxTime;
        weakSelf.timeL.text = [NSString stringWithFormat:@"%@-%@", minTime, maxTime];
    };
    // 核心代码
    self.definesPresentationContext = YES;
    time.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self.navigationController presentViewController:time animated:NO completion:nil];
}

- (IBAction)emailBtnClick:(id)sender {
    SimpleInputVC *input = [[SimpleInputVC alloc] init];
    input.index = 2;
    input.saveBlock = ^(NSString *input) {
        self.emailL.text = input;
    };
    [self.navigationController pushViewController:input animated:YES];
}

- (IBAction)nextBtnClick:(id)sender {
    if (!self.companyL.text || self.companyL.text.length == 0) {
        [CommonUtils showToast:@"请输入公司名称"];
        return;
    }
    if (!self.positionCategoryL.text || self.positionCategoryL.text.length == 0) {
        [CommonUtils showToast:@"请选择职位类别"];
        return;
    }
    if (!self.positionNameL.text || self.positionNameL.text.length == 0) {
        [CommonUtils showToast:@"请输入职位名称"];
        return;
    }
    if (!self.cityL.text || self.cityL.text.length == 0) {
        [CommonUtils showToast:@"请选择工作城市"];
        return;
    }
    if (!self.timeL.text || self.timeL.text.length == 0) {
        [CommonUtils showToast:@"请选择在职时间"];
        return;
    }
    if (!self.emailL.text || self.emailL.text.length == 0) {
        [CommonUtils showToast:@"请输入企业邮箱"];
        return;
    }
    
    if (self.editPage == 1) {
        self.params[@"companyId"] = self.company.companyId;
        self.params[@"jobId"] = self.job.jobId;
        self.params[@"positionName"] = self.positionNameL.text;
        self.params[@"region"] = self.city.regionId;
        
        NSInteger entryTime = 0;
        if ([_maxTime isEqualToString:@"至今"]) {
            NSDateFormatter *ft = [[NSDateFormatter alloc] init];
            [ft setDateFormat:@"YYYY"];
            NSDate *dateNow = [NSDate date];
            NSString *currentTimeStr = [ft stringFromDate:dateNow];
            entryTime = [currentTimeStr integerValue] - [_minTime integerValue];
        } else {
            entryTime = [_maxTime integerValue] - [_minTime integerValue];
        }
        self.params[@"entryTime"] = [NSString stringWithFormat:@"%zd", entryTime];  // 在职时间
        self.params[@"companyEmail"] = self.emailL.text; // 邮箱
        self.editBlock(self.params, self.company.companyAbbreviatedName, self.job.jobName, self.city.regionName, self.minTime, self.maxTime);
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        // 是否实名认证,认证ID
        if (self.authenModel) {
            params[@"realNameAuthentication"] = self.authenModel.realNameAuthentication ? @"1" : @"0";
            params[@"authenticationId"] = self.authenModel.authenticationId;
        }
        params[@"companyId"] = self.company.companyId;
        params[@"jobId"] = self.job.jobId;
        params[@"positionName"] = self.positionNameL.text;
        params[@"region"] = self.city.regionId;
        
        NSInteger entryTime = 0;
        if ([_maxTime isEqualToString:@"至今"]) {
            NSDateFormatter *ft = [[NSDateFormatter alloc] init];
            [ft setDateFormat:@"YYYY"];
            NSDate *dateNow = [NSDate date];
            NSString *currentTimeStr = [ft stringFromDate:dateNow];
            entryTime = [currentTimeStr integerValue] - [_minTime integerValue];
        } else {
            entryTime = [_maxTime integerValue] - [_minTime integerValue];
        }
        params[@"entryTime"] = [NSString stringWithFormat:@"%zd", entryTime];  // 在职时间
        params[@"companyEmail"] = self.emailL.text; // 邮箱
        
        CertificationViewController *vc = [[CertificationViewController alloc] init];
        vc.params = params;
        vc.companyName = self.company.companyAbbreviatedName;
        vc.jobName = self.job.jobName;
        vc.regionName = self.city.regionName;
        vc.minTime = _minTime;
        vc.maxTime = _maxTime;
        vc.index = self.index;
        [self.navigationController pushViewController:vc animated:nil];
    }
}

@end
